package com.ruoyi.bearing.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.bearing.domain.Componentdata;
import com.ruoyi.bearing.service.IComponentdataService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 数据入库Controller
 *
 * @author lx
 * @date 2025-03-19
 */
@RestController
@RequestMapping("/bearing/componentdata")
public class ComponentdataController extends BaseController
{
    @Autowired
    private IComponentdataService componentdataService;

    /**
     * 查询数据入库列表
     */
    @PreAuthorize("@ss.hasPermi('bearing:componentdata:list')")
    @GetMapping("/selectAll")
    public AjaxResult selectAll()
    {
        List<Componentdata> list = componentdataService.selectAll();
        return AjaxResult.success(list);
    }

    /**
     * 查询数据入库列表
     */
    @PreAuthorize("@ss.hasPermi('bearing:componentdata:list')")
    @GetMapping("/list")
    public TableDataInfo list(Componentdata componentdata)
    {
        startPage();
        List<Componentdata> list = componentdataService.selectComponentdataList(componentdata);
        return getDataTable(list);
    }

    /**
     * 导出数据入库列表
     */
    @PreAuthorize("@ss.hasPermi('bearing:componentdata:export')")
    @Log(title = "数据入库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Componentdata componentdata)
    {
        List<Componentdata> list = componentdataService.selectComponentdataList(componentdata);
        ExcelUtil<Componentdata> util = new ExcelUtil<Componentdata>(Componentdata.class);
        util.exportExcel(response, list, "数据入库数据");
    }

    /**
     * 获取数据入库详细信息
     */
    @PreAuthorize("@ss.hasPermi('bearing:componentdata:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(componentdataService.selectComponentdataById(id));
    }

    /**
     * 新增数据入库
     */
    @PreAuthorize("@ss.hasPermi('bearing:componentdata:add')")
    @Log(title = "数据入库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Componentdata componentdata)
    {
        return toAjax(componentdataService.insertComponentdata(componentdata));
    }

    /**
     * 修改数据入库
     */
    @PreAuthorize("@ss.hasPermi('bearing:componentdata:edit')")
    @Log(title = "数据入库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Componentdata componentdata)
    {
        return toAjax(componentdataService.updateComponentdata(componentdata));
    }

    /**
     * 删除数据入库
     */
    @PreAuthorize("@ss.hasPermi('bearing:componentdata:remove')")
    @Log(title = "数据入库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(componentdataService.deleteComponentdataByIds(ids));
    }

    /**
     * 上传文件
     */
    @PreAuthorize("@ss.hasPermi('bearing:componentdata:add')")
    @Log(title = "数据入库", businessType = BusinessType.INSERT)
    @PostMapping("/upload")
    public AjaxResult upload(@RequestParam("file") MultipartFile file, @RequestParam("typeName") String typeName,
                             @RequestParam("labelName") String labelName, @RequestParam("name") String name,@RequestParam("conditionId") Long conditionId,@RequestParam("id") Long id) {
        if (file.isEmpty()) {
            return AjaxResult.error("上传文件不能为空");
        }
        Componentdata componentdata = new Componentdata();
        componentdata.setTypeName(typeName);
        componentdata.setName(name);
        componentdata.setId(id);
        componentdata.setLabelName(labelName);
        componentdata.setConditionId(conditionId);

        int result = componentdataService.uploadFile(file, componentdata);
        if (result > 0) {
            return AjaxResult.success("文件上传成功");
        } else {
            return AjaxResult.error("文件上传失败");
        }
    }

    /**
     * 保存特性文件路径
     */
    @PreAuthorize("@ss.hasPermi('bearing:componentdata:add')")
    @Log(title = "数据入库", businessType = BusinessType.INSERT)
    @PostMapping("/save-characteristic-file-path")
    public AjaxResult saveCharacteristicFilePath(@RequestBody Componentdata componentdata) {
        int result = componentdataService.saveCharacteristicFilePath(componentdata);
        if (result > 0) {
            return AjaxResult.success("特性文件路径保存成功");
        } else {
            return AjaxResult.error("特性文件路径保存失败");
        }
    }
}
